package com.purchase.ml.resource.sys;

import com.purchase.ml.annotations.OperLog;
import com.purchase.ml.bean.base.ApiRestResult;
import com.purchase.ml.bean.req.sys.SysPermissionDto;
import com.purchase.ml.bean.req.sys.SysPermissionEditDto;
import com.purchase.ml.bean.req.sys.SysPermissionNewDto;
import com.purchase.ml.enums.RetStatusNum;
import com.purchase.ml.resource.BaseResource;
import com.purchase.ml.service.sys.SysPermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
* <p>
    * 菜单权限表 前端控制器
    * </p>
*
* @author kyyang
* Created 2021-01-20
*/

@Api(tags = "权限表")
@RestController
@RequestMapping("/permission")
public class SysPermissionResource extends BaseResource {

     @Autowired
     private SysPermissionService sysPermissionService;


    @RequestMapping(method = RequestMethod.POST, path = "/query")
    @ApiOperation("权限查询")
    public ApiRestResult sysPermissionQuery(@RequestBody SysPermissionDto sysPermissionDto) {
        return ApiRestResult.ok(sysPermissionService.sysPermissionByPage(sysPermissionDto));
    }


    @RequestMapping(method = RequestMethod.POST, path = "/new")
    @OperLog(operDesc = "权限新建", operModul = "permission", operType = "new")
    @ApiOperation("权限新建")
    public ApiRestResult sysPermissionNew(@Valid @RequestBody SysPermissionNewDto sysPermissionNewDto ) {

        return ApiRestResult.ok(sysPermissionService.sysPermissionNew(sysPermissionNewDto));
    }


    @RequestMapping(method = RequestMethod.POST, path = "/edit")
    @OperLog(operDesc = "权限编辑", operModul = "permission", operType = "edit")
    @ApiOperation("权限编辑")
    public ApiRestResult sysPermissionEdit(@Valid @RequestBody SysPermissionEditDto sysPermissionEditDto ) {

        return ApiRestResult.ok(sysPermissionService.sysPermissionEdit(sysPermissionEditDto));
    }


    @RequestMapping(method = RequestMethod.DELETE, path = "/del")
    @OperLog(operDesc = "权限删除", operModul = "permission", operType = "del")
    @ApiOperation("权限删除")
    public ApiRestResult sysPermissionDel(@RequestParam("ids") List<Long> ids) {
        if(ids==null||ids.size()<1){
            return ApiRestResult.err(RetStatusNum.PARAMS_ERROR.getMessage(), RetStatusNum.PARAMS_ERROR.getCode());
        }
        return ApiRestResult.ok(sysPermissionService.sysPermissionDel(ids));
    }
}
